<?php

  /*
   * dao.php
   * author: daniel rudigier
   * date: 22.4.08
   * 
   * convert the exported hibernate dao files into 
   * own ocumed dao factory pattern files
   * extract interface and many more
   * 
   * love it or hate it!
   */

  $inputdir = "./ocumed/persistenz/hibernate";
  $outputdir = "./ocumed/persistenz/ready";
  $outputdaodir = "./ocumed/persistenz/readydao";
  $package = "ocumed.persistenz.dao";
    
  // open the directory
  if (is_dir($inputdir)) {
    if ($dh = opendir($inputdir)) {
      while (($file = readdir($dh)) !== false) {
        if (is_file($inputdir . '/' . $file)) {
          
          // xml schemas
          if (strpos($file, '.xml') !== false) {
            $str = file_get_contents($inputdir . '/' . $file);
            $str = str_replace("class=\"assigned\"", "class=\"sequence-identity\"", $str);
            
            $fh = fopen($inputdir . '/' . $file, 'wb');
            fwrite($fh, $str);
            fclose($fh);
          
            //copy ($inputdir . '/' . $file, $outputdir . '/' . $file);
          }
          
          // copy the java class
          //if (strpos($file, '.java') !== false) {
          //  copy ($inputdir . '/' . $file, $outputdir . '/' . $file);
          //}
          
          // modifiy the dao files
          if (strpos($file, 'Home') !== false) {
            $str = file_get_contents($inputdir . '/' . $file);
            unlink($inputdir . '/' . $file);
            
            // assign util class
            $str = str_replace("sessionFactory = getSessionFactory();", 
                               "sessionFactory = HibernateUtil.getSessionFactory();", $str);
             
            // remove the session factory jndi crap
            // add the new methods
            $from = "protected SessionFactory getSessionFactory() {";
            $to = "public void";
            $pos1 = strpos($str, $from);
            $pos2 = strpos($str, $to) - strlen($to) ;
            
            $str = substr($str, 0, $pos1) . substr($str, $pos2 + 5);
            
            // implement the interface
            //public class OcBenutzerHome
            $matches = array();
            preg_match('/Oc(\w+)Home/', $str, $matches);
            $interface = $matches[1]."DAO";
            $class = "Oc".$matches[1];
            $str = preg_replace('/(class Oc\w+Home)/', "\\1 implements $interface", $str);
            
					  // new methods
					  $methods = file_get_contents("methods.txt");
					  $methods = str_replace('$class', $class, $methods);
            $methods = str_replace('$interface', $interface, $methods);
            
            // add the methods, remove previous
            $str = preg_replace('/public List([^\}]*\}){3}/smi', $methods, $str);
            $str = preg_replace('/public void persist([^\}]*\}){3}/smi', "", $str);
            
            // add our project signature            
            $matches = array();
            preg_match('/\/\/ Generated (.*) by (.*)/', $str, $matches);
            $str = preg_replace('/\/\/ Generated (.*) by (.*)/', '', $str);
            
            $sig = file_get_contents("signatureHome.txt");
            $sig = str_replace('$class', $class, $sig);
            $sig = str_replacE('$date', date("d.m.Y"), $sig);
            $sig = str_replace('$hibernate_sig', trim($matches[2]), $sig);
            $sig = str_replace('$hibernate_date', trim($matches[1]), $sig);
            $sig = str_replace('$php', PHP_VERSION, $sig);
            $str = $sig . $str;
            
            //fix imports
            $imports = <<<EOI
import java.util.List;

import $package.$interface;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.LockMode;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;  


EOI;
            $str = preg_replace("/import[^;]*;\s+/smi", "x", $str);
            $str = str_replace("xxxxxxx", $imports, $str);
            
            // rollback
            $rb = <<<EOB
Transaction tx = sessionFactory.getCurrentSession().getTransaction();
            if (tx != null) {
                log.error("executing rollback.");
                tx.rollback();
            }
            throw re;
EOB;
            $str = str_replace("throw re;", $rb, $str);
            
            // 
            $str = preg_replace("/\n\s+\n/smi", "\n\n", $str);
                        
            $fh = fopen($outputdir . '/' . $file, 'wb');
            fwrite($fh, $str);
            fclose($fh);
            
            // step2: extract the dao interface
            $str = file_get_contents("interfaceTemplate.txt");
            $str = str_replace('$class', $class, $str);
            $str = str_replace('$interface', $interface, $str);
            
            $fh = fopen($outputdaodir . '/' . $interface . '.java', 'wb');
            fwrite($fh, $str);
            fclose($fh);
          
            echo "filename: $file : filetype: " . filetype($inputdir . '/' . $file) . "\n";
          }
          
        }
      }
      closedir($dh);
    }
  }

?>